package com.gxa.utils;


import java.util.Calendar;
import java.util.Date;

/**
 * 通过员工出生日期计算员工年龄
 */
public class EmployeeAge {

    /**
     * 通过员工出生日期计算员工年龄
     * @param birthday    员工出生日期
     * @return            返回一个int型的年龄
     */
    public static int getEmployeeAge(Date birthday){
        Calendar calendar = Calendar.getInstance();
        int yearNow = calendar.get(Calendar.YEAR);               //当前年份
        int monthNow = calendar.get(Calendar.MONTH);             //当前月份
        int dayOfMonthNow = calendar.get(Calendar.DAY_OF_MONTH); //当前日期
        calendar.setTime(birthday);
        int yearBirth = calendar.get(Calendar.YEAR);
        int monthBirth = calendar.get(Calendar.MONTH);
        int dayOfMonthBirth = calendar.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;  //当前日期在生日之前，年龄减一
            } else {
                age--;                                       //当前月份在生日之前，年龄减一
            }
        }
        return age;
    }
}
